<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Centrifuge + OAuth2</title>
        <script type="text/javascript" src="https://unpkg.com/centrifuge@^5/dist/centrifuge.js"></script>
    </head>
    <body>
        <div>
            Go to <a href="/account">account page</a>
        </div>
        <div style="margin-top: 20px;">
            <input type="text" id="input" />
        </div>
        <script type="text/javascript">
            const centrifuge = new Centrifuge('ws://localhost:3000/connection/websocket');

            function drawText(text) {
                const div = document.createElement('div');
                div.innerHTML = text;
                document.body.appendChild(div);
            }

            centrifuge.on('connected', function(ctx){
                drawText('Connected over ' + ctx.transport + '<br>');
            });

            centrifuge.on('connecting', function(ctx){
                drawText('Connecting: ' + ctx.reason + '<br>');
            });

            centrifuge.on('disconnected', function(ctx){
                drawText('Disconnected: ' + ctx.reason + '<br>');
            });

            const sub = centrifuge.newSubscription("chat");
            sub.on('publication', function(ctx) {
                drawText(ctx.info.user + ": " + ctx.data + '<br>');
            })
            sub.subscribe();

            const input = document.getElementById("input");
            input.addEventListener('keyup', function(e) {
                if (e.keyCode == 13) {
                    sub.publish(this.value);
                    input.value = '';
                }
            });

            centrifuge.connect();
        </script>
    </body>
</html>
